/*
 * =====================================================================================
 *
 *       Filename:  main.cc
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  2022年01月21日 21时07分04秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  first_name last_name (fl), fl@my-company.com
 *        Company:  my-company
 *
 * =====================================================================================
 */
#include <iostream>

using namespace std;
char* GetMemory1(unsigned long size) {
    char* p = new char[size];   //1. plain new
    return p;
}

char* GetMemory2(unsigned long size) {
    char* p = new (nothrow) char[size]; //2. nothrow new
    return p;
}

class ADT {
  public:
    ADT() {
        cout << "new" << endl;
    } ~ADT() {
        cout << "del" << endl;
    }

  private:
    int i;
    int j;
};

int main() {
#if 0

    try {
        char* p = GetMemory1(10000000111111111);

        //char* p = GetMemory2(10000000111111111);

        if (!p) {
            cout << "P == NULL" << endl;
        }

        delete[]p;
    } catch (const std::bad_alloc& ex) {
        cout << ex.what() << endl;
    }

#endif
    //3. placement new
    char* p = new (nothrow) char[sizeof(ADT) + 2];

    if (p == NULL) {
        cout << "new null" << endl;
    }

    ADT* q = new (p) ADT;
    q->~ADT();     //显式 调用析构函数
    delete[]p;
}
